--
-- Author: shunguo.chen
-- Date: 2019-05-18 18:02:05
--
-- LegendStarRender     传奇星魂
--

local ClassRef = app.mvc.createRenderClass(...)

--data = LegendStar
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    display.setCascadeOpacityEnabled(self._ui.Panel,true)

    display.uiAddClick(self._ui.Panel, function()
        if self._data and not self:isSelected() then
            executeFunc(self._onClick, self)
        end
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local isActive = data:isActive()

        self._ui.icon:loadTexture(data:getIcon())
        self._ui.icon:setOpacity(isActive and 255 or 128)
        self._ui.bg_n:setVisible(isActive)

        self._ui.num:setString(string.format("%s/%s", data.lv, data.maxLv))
    end
end

function ClassRef:setCanActive(b)
    self._ui.bg_light:setVisible(b)
    self._ui.icon_add:setVisible(b)
end

function ClassRef:setSelected(b)
    self._ui.selected:setVisible(b)
end
function ClassRef:isSelected()
    return self._ui.selected:isVisible()
end

function ClassRef:setClickEnable(b)
    self._ui.Panel:setTouchEnabled(b)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
